package com.nightcolor.im;

import com.nightcolor.im.framework.BaseActivity;
import com.nightcolor.im.framework.Utils;
import com.nightcolor.im.view.AdGallery;
import com.nightcolor.im.view.AdItemView;
import com.nightcolor.im.view.ImageWallGridView;
import com.nightcolor.im.view.ImageWallItemView;
import com.nightcolor.im.R;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ScrollView;

public class HomeActivity extends BaseActivity {

	private ImageWallGridView mImageWall;
	private ImageWallAdapter mImageWallAdapter;
	
	private AdGallery mAdGallery;
	private int[] mAdArr = {R.drawable.ad1, 
			R.drawable.ad2, 
			R.drawable.ad3, 
			R.drawable.ad4};
	private AdAdapter mAdAdapter;
	
	private ScrollView mHomeScrollView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mRootView.mTitleBar.setVisibility(View.GONE);
		
		mImageWall = (ImageWallGridView)findViewById(R.id.home_portrait_gridview);
		mAdGallery = (AdGallery)findViewById(R.id.home_image_gallery);
		mHomeScrollView = (ScrollView)findViewById(R.id.home_scroll_view);
		
		mRootView.mMainView.setMenuSlideChangeListener(this);
		mAdGallery.setScrolledMainView(mRootView.mMainView);
		
		mImageWallAdapter = new ImageWallAdapter();
		mImageWall.setAdapter(mImageWallAdapter);
		mImageWall.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent i = new Intent(getApplicationContext(), 
						HorizontalLvTest.class);
				startActivity(i);
			}
			
		});
		
		mAdAdapter = new AdAdapter();
		mAdGallery.setAdapter(mAdAdapter);
		mAdGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent i = new Intent(getApplicationContext(), 
						HorizontalLvTest.class);
				startActivity(i);
			}
			
		});
		
		mHomeScrollView.post(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				mHomeScrollView.scrollTo(0, 0);
			}
		});
	}

	@Override
	protected View genContentView() {
		return Utils.inflateLayout(
				this, R.layout.home_content, null);
	}
	
	@Override
	protected View genLeftMenuView() {
		return Utils.inflateLayout(
				this, R.layout.default_login_sidemenu, null);
	}
	
	@Override
	protected View genBottomBar() {
		return Utils.inflateLayout(
				this, R.layout.home_bottombar, null);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	private class ImageWallAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 18;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				convertView = new ImageWallItemView(getApplicationContext());
			} else {
				((ImageWallItemView)convertView).updateView();
			}
			return convertView;
		}
		
	}
	
	private class AdAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return Integer.MAX_VALUE;
		}
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			mAdGallery.setCurPage(position);
			// TODO Auto-generated method stub
			if (convertView == null) {
				convertView = new AdItemView(getApplicationContext());
			}
			((AdItemView)convertView).updateView(mAdArr[position % mAdArr.length]);
			return convertView;
		}
		
	}
	
	@Override
	public void onSlideChange(boolean isMenuOpen) {
		// TODO Auto-generated method stub
		mAdGallery.setMenuOpen(isMenuOpen);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
}
